package com.example.sharesinfo.bmob;

import android.content.Context;

import com.example.sharesinfo.utils.LogUtils;

import java.io.File;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * @author GC
 * @type
 * @explain Bomb管理类
 * @creat time  2020/3/22
 */
public class BmobManager {
    private volatile static BmobManager mInstance = null;
    private static final String BMOB_SDK_ID = "5673cc03df789d991abfa48dd8a46b9a";
    private static final String LOGTAG = "BmobManager:";

    private BmobManager() {

    }

    public static BmobManager getInstance() {
        if (mInstance == null) {
            synchronized (BmobManager.class) {
                if (mInstance == null) {
                    mInstance = new BmobManager();
                }
            }
        }
        return mInstance;
    }

    /**
     * 初始化Bmob
     */

    public void initBmob(Context mContext) {
        Bmob.initialize(mContext, BMOB_SDK_ID);
    }

    /**
     * 上传头像
     */
    public void uploadHeadPhoto(File uploadFile, final OnUploadPhotoListener listener) {
        final BmobFile file = new BmobFile(uploadFile);
        file.uploadblock(new UploadFileListener() {
            @Override
            public void done(BmobException e) {
                if (e == null) {
                    //上传成功
                    listener.OnSuccess(file.getFileUrl());
                    LogUtils.i(LOGTAG + file.getFileUrl());
                }else {
                    listener.OnFailed(e);
                }
            }
        });
    }

    /**
     * 告诉外部是否成功的接口
     */
    public interface OnUploadPhotoListener {
        void OnSuccess(String imgurl);

        void OnFailed(BmobException e);
    }

}
